package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.land.LandBuyTemplate;
import com.xcity.game.land.LandTemplate;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class LandListResponse extends Response {

	private List<Land> lands;
	
	public static class Land {
		private int buildId;
		private String img;
		private int cost;
		private String name;
		private byte buyType;
		private int level;
		private long money;
		private int cd;
		private long price;
		private long income;
		private long maxIncome;
		private Owner owner;
		public int getBuildId() {
			return buildId;
		}
		public void setBuildId(int buildId) {
			this.buildId = buildId;
		}
		public String getImg() {
			return img;
		}
		public void setImg(String img) {
			this.img = img;
		}
		public int getCost() {
			return cost;
		}
		public void setCost(int cost) {
			this.cost = cost;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public byte getBuyType() {
			return buyType;
		}
		public void setBuyType(byte buyType) {
			this.buyType = buyType;
		}
		public int getLevel() {
			return level;
		}
		public void setLevel(int level) {
			this.level = level;
		}
		public long getMoney() {
			return money;
		}
		public void setMoney(long money) {
			this.money = money;
		}
		public int getCd() {
			return cd;
		}
		public void setCd(int cd) {
			this.cd = cd;
		}
		public long getPrice() {
			return price;
		}
		public void setPrice(long price) {
			this.price = price;
		}
		public long getIncome() {
			return income;
		}
		public void setIncome(long income) {
			this.income = income;
		}
		public long getMaxIncome() {
			return maxIncome;
		}
		public void setMaxIncome(long maxIncome) {
			this.maxIncome = maxIncome;
		}
		public Owner getOwner() {
			return owner;
		}
		public void setOwner(Owner owner) {
			this.owner = owner;
		}
	}
	
	public static class Owner {
		private long ownerId;
		private String ownerName;
		private String ownerHead;
		private boolean isOnLine;
		public long getOwnerId() {
			return ownerId;
		}
		public void setOwnerId(long ownerId) {
			this.ownerId = ownerId;
		}
		public String getOwnerName() {
			return ownerName;
		}
		public void setOwnerName(String ownerName) {
			this.ownerName = ownerName;
		}
		public String getOwnerHead() {
			return ownerHead;
		}
		public void setOwnerHead(String ownerHead) {
			this.ownerHead = ownerHead;
		}
		public boolean isIsOnLine() {
			return isOnLine;
		}
		public void setIsOnLine(boolean isOnLine) {
			this.isOnLine = isOnLine;
		}
	}
	
	public LandListResponse(int serial, Collection<com.xcity.db.entity.Land> srcs) {
		super(serial);
		List<Land> lands = new ArrayList<Land>(srcs.size());
		PlayerCacheService as = App.getApp().getServiceManager().get(PlayerCacheService.class);
		for (com.xcity.db.entity.Land src : srcs) {
			LandTemplate template = src.getTemplate();
			LandBuyTemplate nextLvlTemp = template.getBuyTemplate(src.getLevel() + 1);
			LandBuyTemplate nowLvlTemp = template.getBuyTemplate(src.getLevel());
			Land land = new Land();
			land.buildId = src.getId();
			land.img = template.getImage();
			land.cost = 0;
			land.name = template.getName();
			land.buyType = template.getCostType();
			land.level = src.getLevel();
			land.money = src.calcMoney();
			land.cd = src.getCD();
			land.price = nextLvlTemp.getCost();
			land.income = nowLvlTemp.getIncome();
			land.maxIncome = nowLvlTemp.getMaxIncome();
			if (src.getOwnerId() != 0) {
				Player actor = as.find(src.getOwnerId());
				land.owner = new Owner();
				land.owner.ownerId = actor.getId();
				land.owner.ownerName = actor.getName();
				land.owner.ownerHead = actor.getIcon();
				land.owner.isOnLine = actor.isOnline();
			}
			lands.add(land);
		}
		this.lands = lands;
	}

	public List<Land> getLands() {
		return lands;
	}

	public void setLands(List<Land> lands) {
		this.lands = lands;
	}
	
}
